package business;

import db.GitCountDao;
import db.PropertiesDao;
import db.TruncateGitCountDao;
import gitutil.GitUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pojo.Properties;

import java.io.File;
import java.util.List;

/**
 * @Description:
 * @Author: xiaobing
 * @system name: 工作流引擎
 * @copyright：长安新生（深圳）金融投资有限公司
 * @Date: Created in  2018/9/26 15:36
 */
public class BusinessLogic {

    /**用户名*/
    public static String  username = "xiaobing@caxins.com";
    /**密码*/
    public static String password = "a12345678";

    private static final Logger logger = LoggerFactory.getLogger(BusinessLogic.class);


    public static void BsinessLogic() {
        try {
            // 执行清空语句
            TruncateGitCountDao.gitCountTruncate();

            // 查询配置文件信息
            List<Properties> list = PropertiesDao.queryProperties();
            for (Properties properties : list) {
                String projectCde = properties.getProjectCde().trim();
                String remotePath = properties.getRemotePath().trim();
                String projectBranch = properties.getProjectBranch().trim();
                String downloadPath = properties.getDownloadPath().trim();
                String localPath = downloadPath + projectCde;

                File file = new File(localPath);
                // 如果文件夹不存在则创建，否则直接pull
                if (!file.exists() && !file.isDirectory()) {
                    logger.info(projectCde + "项目不存在！正在进行创建并克隆此项目。。。");
                    file.mkdir();
                    GitUtil.gitClone(username,password,remotePath,projectBranch,localPath);
                    logger.info(projectCde + "项目创建并克隆完成！");
                } else {
                    GitUtil.gitPull(username,password,localPath,projectBranch);
                    logger.info(projectCde + "项目pull完成！");
                }

                try {
                    // 统计并插入数据
                    GitCountDao.gitCountBatchSave(localPath,projectBranch,projectCde);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("项目插入失败！");
        }

    }


}
